package org.xhnews.log;

import java.util.Date;

import net.rim.device.api.system.EventLogger;

public class RaspberryLogger {

	private static final String SEPARATOR = "  ";
	private static long GUID = 0x5722c0170198caeaL;
	private static final String APP_NAME = "raspberry";

	RaspberryLogger() {
		EventLogger.register(GUID, APP_NAME, EventLogger.VIEWER_STRING);
	}

	private static RaspberryLogger logger = new RaspberryLogger();

	public static RaspberryLogger getInstance() {
		return logger;
	}

	/*
	 * Log the information.
	 * 
	 * @param message The message you will log
	 */
	public void info(String message) {
		if (message == null || message.trim().equals(""))
			return;
		String log = "INFO: " + new Date().toString() + SEPARATOR + message;
		System.out.println("\n###############begin################");
		System.out.println(message);
		System.out.println("###############end##################");
		try {
			log = new String(log.getBytes(), "UTF-8");
			byte[] msg_bytes = log.getBytes();
			EventLogger.logEvent(GUID, msg_bytes, EventLogger.ALWAYS_LOG);
		} catch (Exception ex) {
			System.err.println(ex);
		}
	}

	public void error(Exception e) {
		StringBuffer log = new StringBuffer();
		log.append("ERROR: " + new Date().toString() + SEPARATOR);
		log.append(e.getMessage() + " \r\n ");
		System.out.println("\n###############begin################");
		System.out.println(log);
		System.out.println("###############end##################");
		try {
			byte[] msg_bytes = new String(log.toString().getBytes(), "UTF-8")
					.getBytes();
			EventLogger.logEvent(GUID, msg_bytes);
		} catch (Exception ex) {
			System.err.println(ex);
		}
	}

	public void error(String message, Exception e) {
		StringBuffer log = new StringBuffer();
		log.append("ERROR: " + new Date().toString() + SEPARATOR);
		if (message != null || !message.trim().equals("")) {
			log.append("message: " + message + " \r\n ");
		}
		log.append("error: " + e.getMessage() + " \r\n ");
		System.out.println("\n###############begin################");
		System.out.println(log);
		System.out.println("###############end##################");
		try {
			byte[] msg_bytes = new String(log.toString().getBytes(), "UTF-8")
					.getBytes();
			EventLogger.logEvent(GUID, msg_bytes);
		} catch (Exception ex) {
			System.err.println(ex);
		}
	}

}
